#include <Windows.h>
#include <tchar.h>
#include "resource.h"
#include "./../DllLib/resource.h"

int CALLBACK DlgProc2(HWND hDlg, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	switch (nMsg)
	{
		case WM_INITDIALOG:
		{
			TCHAR* szContent = (TCHAR*)TlsGetValue(lParam);
			if (NULL != szContent)
			{
				SetDlgItemText(hDlg, IDC_STCCONTENT, szContent);
			}
			break;
		}
		case WM_COMMAND:
		{
			switch (LOWORD(wParam))
			{
				case IDC_BTNEXIT:
				{
					EndDialog(hDlg, 0);
					break;
				}
			}
		}
		case WM_CLOSE:
		case WM_DESTROY:
		{
			EndDialog(hDlg, 0);
			break;
		}
		break;
	}

	return 0;
}

int CALLBACK DlgProc(HWND hDlg, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	static HMODULE  hModule = NULL;
	static HICON	hIcon	= NULL;
	static HBITMAP	hBmp	= NULL;

	switch (nMsg)
	{
		case WM_INITDIALOG:
		{
			SetWindowText(hDlg, _T("Demo0121"));

			UINT nWidth		= GetSystemMetrics(SM_CXSCREEN);
			UINT nHeight	= GetSystemMetrics(SM_CYSCREEN);
			RECT rtClient;
			GetClientRect(hDlg, &rtClient);
			SetWindowPos(hDlg, NULL, (nWidth - (rtClient.right - rtClient.left)) / 2, (nHeight - (rtClient.bottom - rtClient.top)) / 2, 0, 0, SWP_NOSIZE);
			
			hModule = LoadLibrary(_T("DllLib.dll"));

			hIcon = LoadIcon(hModule, MAKEINTRESOURCE(IDI_ICON1));
			hBmp  = LoadBitmap(hModule, MAKEINTRESOURCE(IDB_BITMAP1));
			
			//SendMessage(GetDlgItem(hDlg, IDC_STCICON), STM_SETICON, (WPARAM)hIcon, (LPARAM)0);
			SendMessage(GetDlgItem(hDlg, IDC_STCICON), STM_SETIMAGE, (WPARAM)IMAGE_ICON, (LPARAM)hIcon);
			SendMessage(GetDlgItem(hDlg, IDC_STCBMP), STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hBmp);

			break;
		}
		case WM_COMMAND:
		{
			switch (LOWORD(wParam))
			{
				case IDC_BTNWND:
				{
					HANDLE hHeap = HeapCreate(0, 0, 0);
					TCHAR* pTmp = (TCHAR*)HeapAlloc(hHeap, 0, sizeof(TCHAR) * MAX_PATH);
					LoadString(hModule, IDS_STRING101, pTmp, MAX_PATH);
					DWORD dwIndex = TlsAlloc();
					TlsSetValue(dwIndex, pTmp);
					DialogBoxParam(hModule, MAKEINTRESOURCE(IDD_DIALOG2), hDlg, DlgProc2, dwIndex);	
					TlsFree(dwIndex);
					HeapFree(hHeap, 0, pTmp);
					HeapDestroy(hHeap);
					
					break;
				}
			}
			break;
		}
		case WM_CLOSE:
		{
			DestroyIcon(hIcon);
			DeleteObject(hBmp);
			FreeLibrary(hModule);

			PostQuitMessage(0);
			break;
		}
	}

	return 0;
}

BOOL WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPTSTR szCmdLine, int nShowCmd)
{
	DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG), NULL, DlgProc);

	return TRUE;
}